package com.red.battleworm.model {
	import com.red.battleworm.AppMain;	
	import com.red.battleworm.map.Map;	
	
	import org.puremvc.as3.patterns.proxy.Proxy;
	import org.cove.ape.*;

	import flash.display.Sprite;
	import flash.display.DisplayObject;	
	import flash.utils.getQualifiedClassName;

	/**
	 * @author zhangs
	 */
	public class MapProxy extends Proxy {
//		private static const IMAGE_NAME : String = "image";
		private static const SPRITE_NAME : String = "ape";
		private static const RECTANGLE : String = "Rectangle";
		private static const CIRCLE : String = "Circle";
		private static const BIRTH : String = "BIRTH";
		public static const COLOR : uint = 0x000000;
		
		private var bound : Group;
		private var map : Map;

		public function MapProxy(proxyName : String = null, data : Object = null) {
			super(proxyName, data);
			
			bound = new Group();
			map = new Map();
			init();
		}
		
		public function init() : void {
			var map_sprite : Sprite = Sprite(this.getData());
//			var image : DisplayObject = map.getChildByName(IMAGE_NAME);
			var sprite : Sprite = Sprite(map_sprite.getChildByName(SPRITE_NAME));
			var obj : DisplayObject;
			for (var i : uint = 0; i < sprite.numChildren; i++) {
				obj = sprite.getChildAt(i);
				add_ape_Particle(obj);
			}
			var width : Number = AppMain.stage.stageWidth;
			var height : Number = AppMain.stage.stageHeight;
			trace(width, height);
			var topRectangleParticle : RectangleParticle = new RectangleParticle(width/2, -20, width, 40, 0, true);
			topRectangleParticle.setStyle(0, COLOR, 1, COLOR);
			var bottomRectangleParticle : RectangleParticle = new RectangleParticle(width/2, height+20, width, 40, 0, true);
			bottomRectangleParticle.setStyle(0, COLOR, 1, COLOR);
			var leftRectangleParticle : RectangleParticle = new RectangleParticle(-20, height/2, 40, height, 0, true);
			leftRectangleParticle.setStyle(0, COLOR, 1, COLOR);
			var rightRectangleParticle : RectangleParticle = new RectangleParticle(width+20, height/2, 40, height, 0, true);
			rightRectangleParticle.setStyle(0, COLOR, 1, COLOR);
			bound.addParticle(topRectangleParticle);
			bound.addParticle(bottomRectangleParticle);
			bound.addParticle(leftRectangleParticle);
			bound.addParticle(rightRectangleParticle);
			trace(this.getProxyName(), "init");
		}
		
		private function add_ape_Particle(obj : DisplayObject) : void {
			switch(getQualifiedClassName(obj)) {
				case RECTANGLE:
					var rotation : Number = obj.rotation / 180 * Math.PI;
//					var sin : Number = Math.abs(Math.sin(rotation));
//					var cos : Number = Math.abs(Math.cos(rotation));
//					var width : Number = Math.abs((obj.width * cos - obj.height * sin) / (cos * cos - sin * sin));
//					var height : Number = Math.abs((obj.width * sin - obj.height * cos) / (cos * cos - sin * sin));
					obj.rotation = 0;
					var width : Number = obj.width;
					var height : Number = obj.height;
					trace(obj.name);
					if(obj.name && obj.name.toUpperCase().indexOf(BIRTH) >= 0) {
						map.birth_array.push({x:obj.x, y:obj.y, width:width, height:height, rotation:rotation});
					} else {
						var aRectangleParticle : RectangleParticle = new RectangleParticle(obj.x, obj.y, width, height, rotation, true);
						aRectangleParticle.setStyle(0, COLOR, 1, COLOR);
						map.addParticle(aRectangleParticle);
					}
					break;
				case CIRCLE:
					var aCircleParticle : CircleParticle = new CircleParticle(obj.x, obj.y, (obj.width + obj.height) / 4, true);
					aCircleParticle.setStyle(0, COLOR, 1, COLOR);
					map.addParticle(aCircleParticle);
					break;
				default:
					trace("Can't resolve a object in add_ape_Particle.");
			}
		}
		
		public function getBound() : Group {
			return bound;
		}

		public function getMap() : Map {
			return map;
		}
	}
}
